package me.buick.util.jmeter.snmp.snmpmgmt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.buick.util.snmp.core.AbstractPerfDAOFactory;
import me.buick.util.snmp.core.SupportedOS;
import me.buick.util.snmp.core.SupportedProtocol;
import me.buick.util.snmp.core.dao.DiskInfoDAO;
import me.buick.util.snmp.core.dao.NetworkIfInfoDAO;
import me.buick.util.snmp.core.dao.SystemInfoDAO;
import me.buick.util.snmp.core.pojo.CpuDetailedInfoPojo;
import me.buick.util.snmp.core.pojo.CpuOverviewInfoPojo;
import me.buick.util.snmp.core.pojo.DiskIOPojo;
import me.buick.util.snmp.core.pojo.DiskUsagePojo;
import me.buick.util.snmp.core.pojo.MemoryInfoPojo;
import me.buick.util.snmp.core.pojo.NetworkInfoPojo;
import me.buick.util.snmp.core.pojo.SystemInfoPojo;

/**
 * 
 * Version 1.0
 * 
 * This class is used to be a communication adaptor between SNMPSampler and Buick SNMP Utility API.
 * 
 * Every SNMPSampler thread has their own SNMPdataAdaptor instance until this Sampler is shut down.
 * 
 * @author buick
 *
 */

public class SNMPDataAdaptor {
	
	SNMPSampleResult sampleResult = new SNMPSampleResult();
	AbstractPerfDAOFactory snmpDAOFactory = null;
	DiskInfoDAO      diskInfoDAO = null;
	NetworkIfInfoDAO nerworkInfoDAO = null;
	SystemInfoDAO    systemInfoDAO = null;
	
	String server;
	int port = 161;
	boolean	flag = false;
	
		
	public SNMPDataAdaptor(SupportedOS os, String server, int port, int snmpDataRetriveInterval) {
		this.server = server;
		this.port = port;
		snmpDAOFactory = AbstractPerfDAOFactory.getPerfDAOFactory(os, SupportedProtocol.SNMP, server, port, snmpDataRetriveInterval);
		diskInfoDAO = snmpDAOFactory.getDiskInfoDAO();
		nerworkInfoDAO = snmpDAOFactory.getNetworkIfInfoDAO();
		systemInfoDAO = snmpDAOFactory.getSystemInfoDAO();
	} 
	
	/**
	 * Read the Performance data from remote target and then fill these data into SNMPSampelResult.
	 * 
	 *  All the data we get from Buick snmp utility can be set to the sample result directly except the CPU detailed Data
	 * 
	 * @param sampleResult
	 */
	public void fillSampleResult(SNMPSampleResult sampleResult) {
		
			if(!flag){
				flag = true;
				try {
					Thread.sleep(Long.parseLong(sampleResult.getSnmpDataRetriveInterval()));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			Map<String, DiskIOPojo>    diskIOPojoMap = diskInfoDAO.getDiskIOInfo();
			Map<String, DiskUsagePojo> diskUsagePojoMap  = diskInfoDAO.getDiskUsageInfo();
			Map<String, NetworkInfoPojo> networkInfoPojoMap = nerworkInfoDAO.getNetworkInfo();
			List<CpuDetailedInfoPojo> cpuDetailedInfoPojoList = systemInfoDAO.getCPUDetailed();
			CpuOverviewInfoPojo cpuOverviewInfoPojo = systemInfoDAO.getCPUOverview();
			SystemInfoPojo systemInfoPojo = systemInfoDAO.getSystemInfoPojo();
			MemoryInfoPojo memoryInfoPojo = systemInfoDAO.getMemoryInfoPojo();
			
			sampleResult.setUpTime(systemInfoPojo.getSystemUpTime());
			
			sampleResult.setCpuLoad(cpuOverviewInfoPojo.getCpuLoad());
				
			Map<String, Double> tempCPUDetailed = new HashMap<String, Double>();
			for (CpuDetailedInfoPojo p : cpuDetailedInfoPojoList) {
				tempCPUDetailed.put("Processor OID "
						+ p.getCpuID(), Double.valueOf(p.getCpuLoad()));
			}
			sampleResult.setProcessorLoad(tempCPUDetailed);
				
			sampleResult.setNetworkIO(networkInfoPojoMap);
				
			sampleResult.setMem(memoryInfoPojo.getMemSizesBytes());
			sampleResult.setMemUsage(memoryInfoPojo.getMemUsageBytes());
			sampleResult.setMemLoad(memoryInfoPojo.getMemLoadPerc());

			sampleResult.setDiskPartitions(diskUsagePojoMap);

			sampleResult.setDiskPartitionsIO(diskIOPojoMap);
	}
	
	/**
	 * As well as the Buick SNMP Utility will start some background thread to feed the SNMP data to caller, so, when the
	 * called are stopped, this method is used to discard remote background threads
	 */
	public void discardDAOs(){
		if(this.snmpDAOFactory != null){
			this.snmpDAOFactory.discardDAOs();
		}
	}

}
